package feng.demo.controller.adm;

import feng.demo.entity.Cart;
import feng.demo.entity.User;
import feng.demo.entity.XJson;
import feng.demo.service.CartService;
import feng.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adm/cart")
public class AdmCartController {

    @Autowired
    CartService cartService;
    @Autowired
    UserService userService;

    @GetMapping("/findAll/{p}/{l}")
    public String findAll(@PathVariable("p")Integer p,@PathVariable("l")Integer l){
        Map<String,Object> map=new HashMap<>();
        List<Cart> list = cartService.findAllLimit(p, l);
        int count = cartService.findCount();
        List<User> users = userService.findByPermid(3);
        map.put("cart",list);
        map.put("count",count);
        map.put("user",users);
        return new XJson(200,"",map).toString();
    }

    @GetMapping("/del/{id}/{p}/{l}")
    public String del(@PathVariable("id")Integer cartid,@PathVariable("p")Integer p,@PathVariable("l")Integer l){
        cartService.del(cartid);
        Map<String,Object> maps=new HashMap<>();
        List<Cart> list = cartService.findAllLimit(p, l);
        int count = cartService.findCount();
        maps.put("cart",list);
        maps.put("count",count);
        return new XJson(200,"删除成功",maps).toString();
    }

    @GetMapping("/findByUid/{id}")
    public String findByuid(@PathVariable("id")Integer uid){
        List<Cart> carts = cartService.findByUid(uid);

        return new XJson(200,"",carts).toString();
    }
}
